package models

import (
	"github.com/astaxie/beego/orm"
)

type AuthUser struct {
	// Id       int    `orm:"column(ID);pk"`
	Username string
	Password string
}

type AuthUserRole struct {
	Username   string
	Name       string
	Email      string
	Phone      string
	Reallyname string
	Userid     string
	Roleid     string
	TenantId   string
	Rolename   string
	Rolecode   string
	Roles      []string
}

type AuthMenuData struct {
	Id       int64
	Parentid int64
	Name     string
	Type     string
	Roleid   int64
	Menuid   int64
	Children []*AuthMenuData
}

type AuthMenu struct {
	Id       int64
	Parentid int64
	Name     string
	Url      string
	Perms    string
	Type     string
	Icon     string
	Ordernum string
	Children []*AuthMenu
}

// GetTSYSUSERById retrieves TSYSUSER by Id. Returns error if
// Id doesn't exist
func GetAuthUserByUserName(user *AuthUser) []orm.ParamsList {
	o := orm.NewOrm()
	var lsits []orm.ParamsList
	o.Raw("SELECT username,salt,password FROM t_sys_user WHERE username = ? ", user.Username).ValuesList(&lsits)
	return lsits
}

func GetAuthUserAndRoleByUserName(username string) ([]AuthUserRole, error) {
	sql := `SELECT u.username,u.name as reallyName,u.email,u.tenant as tenantid,u.phone,r.name as roleName,r.code as rolecode,u.id as userId,r.id as roleId
			 FROM public.t_sys_user u,public.t_sys_user_role ur,public.t_sys_role r
			 WHERE u.id = ur.user_id and ur.role_id = r.id and u.username = ?`
	o := orm.NewOrm()
	var urlist []AuthUserRole
	_, err := o.Raw(sql, username).QueryRows(&urlist)
	return urlist, err
}

func GetAuthRoleMenusByRoleCode(rolecode string) ([]AuthMenu, error) {
	sql := `SELECT
				m.id,
				m.parent_id as parentid,
				m.url,
				m.perms,
				m.type,
				m.icon,
				m.name,
				m.order_num as ordernum
			FROM
				t_sys_role r,
				t_sys_role_menu rm,
				t_sys_menu m
			WHERE
				r.id = rm.role_id
			AND
				rm.menu_id = m.id
			AND
				r.code = ?`
	o := orm.NewOrm()
	var menuList []AuthMenu
	var err error
	_, err = o.Raw(sql, rolecode).QueryRows(&menuList)
	return menuList, err
}

func GetAuthRoleMenuDateByRoleCode(rolecode string) ([]AuthMenuData, error) {
	sql := `SELECT
				rm.id,
				m.parent_id as parentid,
        rm.role_id as roleid,
        rm.menu_id as menuid,
				rm.type,
				m.name
			FROM
				t_sys_role r,
				t_sys_role_menu rm,
				t_sys_menu m
			WHERE
				r.id = rm.role_id
			AND
				(m.type = 1 or m.type = 0 )
			AND
				rm.menu_id = m.id
			AND
				r.code = ?`
	o := orm.NewOrm()
	var menuList []AuthMenuData
	var err error
	_, err = o.Raw(sql, rolecode).QueryRows(&menuList)
	return menuList, err
}

func GetAuthRoleMenuByRoleCode(rolecode string) ([]AuthMenu, error) {
	sql := `SELECT
				m.id,
				m.parent_id as parentid,
				m.url,
				m.perms,
				m.type,
				m.icon,
				m.name,
				m.order_num as ordernum
			FROM
				t_sys_role r,
				t_sys_role_menu rm,
				t_sys_menu m
			WHERE
				r.id = rm.role_id
			AND
				(m.type = 1 or m.type = 0 )
			AND
				rm.menu_id = m.id
			AND
				r.code = ?`
	sqlAdmin := `SELECT
					m.id,
					m.parent_id as parentid,
					m.url,
					m.perms,
					m.type,
					m.icon,
					m.name,
					m.order_num as ordernum
				FROM
					t_sys_menu m
				WHERE
					(m.type = 1 or m.type = 0 )`
	o := orm.NewOrm()
	var menuList []AuthMenu
	var err error
	if rolecode != "admin" {
		_, err = o.Raw(sql, rolecode).QueryRows(&menuList)
	} else {
		_, err = o.Raw(sqlAdmin).QueryRows(&menuList)
	}
	return menuList, err
}
